package com.example.dubbo;

import com.example.dubbo.service.HelloService;
import com.example.dubbo.consumer.service.ConsumerOrderService;
import lombok.SneakyThrows;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.rpc.RpcContext;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CompletableFuture;

/**
 * @author zxs
 * @since 2019/11/20
 */
@EnableDubbo
@MapperScan("com.example")
@SpringBootApplication
public class DubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(DubboConsumerApplication.class);
        //application.setWebApplicationType(WebApplicationType.NONE);
        application.run(args);

        //SpringApplication.run(DubboConsumerApplication.class,args);
    }

    @RestController
    static class HelloDubboController{

        @DubboReference(check = false,mock ="com.example.dubbo.consumer.mock.HelloServiceMock")
        private HelloService helloService;

        @Autowired
        private ConsumerOrderService orderService;

        @SneakyThrows
        @GetMapping("/hello")
        public String hello(String param){
            RpcContext.getContext().setAttachment("param",param);
            CompletableFuture<String> future = RpcContext.getContext().asyncCall(()-> helloService.hello("test"));
            System.out.println(Thread.currentThread()+" do other things");
            String result = future.get();
            System.out.println(Thread.currentThread()+" result:"+result);
            return result;
        }

        @SneakyThrows
        @GetMapping("/order")
        public String createOrder(String userId,String orderType){
            Long id = orderService.createOrder(userId,orderType);
            return "success";
        }
    }
}
